package com.example.leave;
import javax.swing.*;
import javax.swing.SpinnerDateModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import javax.swing.table.DefaultTableModel;

public class Student {
	private String studentId;
	protected Counselor counselor;

    // Constructor that accepts studentId
    public Student(String studentId) {
        this.studentId = studentId;
        createAndShowGUI();
    }

    private void createAndShowGUI() {
        // 创建主窗口
        JFrame frame = new JFrame("请假页面");

        // 创建面板
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        // 创建查询面板
        JPanel queryPanel = new JPanel();
        queryPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        JLabel idLabel = new JLabel("学号：");
        JTextField idText = new JTextField(10);
        idText.setText(studentId);

        // 日期选择器
        JLabel startDateLabel = new JLabel("开始时间：");
        JSpinner startSpinner = new JSpinner(new SpinnerDateModel());
        JSpinner.DateEditor startEditor = new JSpinner.DateEditor(startSpinner, "yyyy.MM.dd HH:mm");
        startSpinner.setEditor(startEditor);

        JLabel endDateLabel = new JLabel("结束时间：");
        JSpinner endSpinner = new JSpinner(new SpinnerDateModel());
        JSpinner.DateEditor endEditor = new JSpinner.DateEditor(endSpinner, "yyyy.MM.dd HH:mm");
        endSpinner.setEditor(endEditor);

        JButton queryButton = new JButton("查询");

        // 创建表格面板
        JPanel tablePanel = new JPanel();
        tablePanel.setLayout(new BorderLayout());
        String[] columnNames = {"学号", "姓名", "请假时间", "状态"};
        DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
        JTable table = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(table);
        tablePanel.add(scrollPane, BorderLayout.CENTER);

        // 查询按钮事件
        queryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 模拟查询数据
                String id = idText.getText();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm");
                String startDate = sdf.format(startSpinner.getValue());
                String endDate = sdf.format(endSpinner.getValue());
                tableModel.setRowCount(0); // 清空旧数据
                tableModel.addRow(new Object[]{id, "张三", startDate + " - " + endDate, "已批准"}); // 示例数据
            }
        });

        // 查看和修改按钮
        JPanel actionPanel = new JPanel();
        actionPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); // 居中对齐
        JButton leaveButton = new JButton("请假");
        JButton viewButton = new JButton("查看");
        JButton editButton = new JButton("修改");
        leaveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new StudentLeave(studentId,counselor); // 创建并显示新的页面
                frame.dispose();
            }
           
        });

        // 添加按钮到动作面板
        actionPanel.add(leaveButton);
        actionPanel.add(viewButton);
        actionPanel.add(editButton);

        // 添加组件到主面板
        queryPanel.add(idLabel);
        queryPanel.add(idText);
        queryPanel.add(startDateLabel);
        queryPanel.add(startSpinner);
        queryPanel.add(endDateLabel);
        queryPanel.add(endSpinner);
        queryPanel.add(queryButton);

        panel.add(queryPanel);
        panel.add(tablePanel);
        panel.add(actionPanel);

        // 设置窗口属性
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 400);
        frame.add(panel);
        frame.setVisible(true);
        
    }
}
